package com.hdu.clj.dncard

import android.app.Activity
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import android.view.animation.TranslateAnimation
import androidx.appcompat.app.AlertDialog
import com.hdu.clj.dncard.databaseUtil.AllEquipDatabaseUtil
import com.hdu.clj.dncard.databaseUtil.HeroDatabaseUtil
import com.hdu.clj.dncard.databaseUtil.UserDatabaseUtil
import com.hdu.clj.dncard.util.GetEquip
import com.hdu.clj.dncard.util.LoadAndSave
import com.hdu.clj.dncard.util.RandomSkill
import kotlinx.android.synthetic.main.activity_map3.*

class Map4Activity : AppCompatActivity() {

    private val skilllist = arrayOf("card1","card2","card3","card4","card5","card6","card7","card8","card9")

//    val hprog = h3prog
//    val m2UserName = m3UserName
//    val m2HeroImage = m3HeroImage
//    val h2textb = h3textb
//    val m2start = m3start
//    val m2gprog = m3gprog
//    val map2gw = map3gw
//    val m2gb = m3gb
//    val m2textb = m3textb
//    val map2card1 = map3card1
//    val map2card2 = map3card2
//    val map2card3 = map3card3
//    val m2hurt = m3hurt
//    val m2heroadd = m3heroadd
//    val h2prog = h3prog
//    val m2exit = m3exit
    val map = "4"
    val lv = 4
    val mg1 = "m3g1"
    val mg2 = "m3g2"
    val mg3 = "m3g3"
    val image_mg1 = R.drawable.m4g1
    val image_mg2 = R.drawable.m4g2
    val image_mg3 = R.drawable.m4g3
    val max_gwlife = 15000
    val exp = 2000



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map4)
        hprog.progress = hprog.progress+100
        /** m1g1 m1g2 m1g3 表示地图1的三只怪物
         * 标记怪物是否存活
         */
        LoadAndSave().save("true",mg1,this)
        LoadAndSave().save("true",mg2,this)
        LoadAndSave().save("true",mg3,this)
        LoadAndSave().save("$max_gwlife","max_gwlife",this)
        val hero_max = LoadAndSave().load("maxlife",this)
        val cur_acc = LoadAndSave().load("userAcc",this)
        val cur_name = LoadAndSave().load("name",this)
        m2UserName.setText(cur_name)
        val cur_hid = HeroDatabaseUtil().findHero(this,cur_acc)
        if (cur_hid==1){
            m2HeroImage.setImageResource(R.drawable.hero0)
        }
        else{
            m2HeroImage.setImageResource(R.drawable.hero1)
        }
        m2UserName.setText(cur_name)
        h2textb.setText("$hero_max/$hero_max")

        var cur_attack = LoadAndSave().load("hattack",this)
        var cur_widom = LoadAndSave().load("hwisdom",this)
        var cur_life = LoadAndSave().load("hlife",this)

        LoadAndSave().save("$max_gwlife",mg1+"life",this)
        LoadAndSave().save("$max_gwlife",mg2+"life",this)
        LoadAndSave().save("$max_gwlife",mg3+"life",this)
        var m1g1_life = LoadAndSave().load("m1g1life",this)


        m2start.setOnClickListener {
            val flagg1 = LoadAndSave().load(mg1,this)
            val flagg2 = LoadAndSave().load(mg2,this)
            val flagg3 = LoadAndSave().load(mg3,this)
            m2gprog.visibility = View.VISIBLE
            if (flagg1.equals("true")){
                LoadAndSave().save(mg1,"cur_fight",this)
                map2gw.setImageResource(image_mg1)
                m2gprog.progress = 100
                m2gb.setText("生命值")
                m2textb.setText("$max_gwlife/$max_gwlife")
//                val cur_skill = RandomSkill().showSkill()
//                map1card1.setText(cur_skill[0])
//                map1card1.setTextColor(Color.parseColor(cur_skill[1]))
//                map1card2.setText(cur_skill[2])
//                map1card2.setTextColor(Color.parseColor(cur_skill[3]))
//                map1card3.setText(cur_skill[4])
//                map1card3.setTextColor(Color.parseColor(cur_skill[5]))
            }
            else if (flagg2.equals("true")){
                map2gw.visibility = View.VISIBLE
                map2gw.setImageResource(image_mg2)
                m2gprog.progress = 100
                m2gb.setText("生命值")
                LoadAndSave().save(mg2,"cur_fight",this)

                m2textb.setText("$max_gwlife/$max_gwlife")

            }
            else if(flagg3.equals("true")){
                map2gw.visibility = View.VISIBLE
                map2gw.setImageResource(image_mg3)
                m2gprog.progress = 100
                m2gb.setText("生命值")
                LoadAndSave().save(mg3,"cur_fight",this)

                m2textb.setText("$max_gwlife/$max_gwlife")
            }
            else{
                //胜利


            }
            val cur_skill = RandomSkill().showSkill(this)
            map2card1.setText(cur_skill[0])
            map2card1.setTextColor(Color.parseColor(cur_skill[1]))
            map2card2.setText(cur_skill[2])
            map2card2.setTextColor(Color.parseColor(cur_skill[3]))
            map2card3.setText(cur_skill[4])
            map2card3.setTextColor(Color.parseColor(cur_skill[5]))
            m2start.isClickable = false
            m2start.isEnabled = false
            m2start.setTextColor(Color.parseColor("#919AA2"))
            map2card1.visibility = View.VISIBLE
            map2card2.visibility = View.VISIBLE
            map2card3.visibility = View.VISIBLE
            map2card2.setBackgroundResource(R.drawable.card)
            map2card1.setBackgroundResource(R.drawable.card)
            map2card3.setBackgroundResource(R.drawable.card)
            map2card1.isClickable=true
            map2card1.isEnabled=true
            map2card2.isClickable=true
            map2card2.isEnabled=true
            map2card3.isClickable=true
            map2card3.isEnabled=true
        }

        map2card1.setOnClickListener {
            map2card1.visibility = View.INVISIBLE
            val res1 = RandomSkill().action(1,this)
            val cur_fight = LoadAndSave().load("cur_fight",this)
            if (res1<0){
                m2hurt.setText(res1.toString())
                Handler().postDelayed({
                    m2hurt.setText("")
                },400)
            }
            else{
                m2heroadd.setText("+$res1")
                Handler().postDelayed({
                    m2heroadd.setText("")
                },400)
            }

            cur_life = LoadAndSave().load("hlife",this)
            m1g1_life = LoadAndSave().load(cur_fight+"life",this)
            m2gprog.progress = (m1g1_life.toInt()*100)/max_gwlife
//            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())

            //显示血量数值
            h2textb.setText("$cur_life/$hero_max")
            m2textb.setText("$m1g1_life/$max_gwlife")
            val cur_finish = RandomSkill().finish(this)
            if (cur_finish>0){
                if (cur_finish==3){
                    LoadAndSave().save("true",map,this)
                    //完成挑战，随机爆装备
                    m2start.setText("完成")
                    m2exit.setText("退出")
                    if (GetEquip().isGetEquip()){
                        //随机装备
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val ename = GetEquip().randomEquip(this,lv)
                        val e_name = ename[0]
                        val e_id = ename[1]
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你额外获得了装备: $e_name,请选择是否穿戴")
                            setNegativeButton("丢弃"){_,_->
                                activity.finish()
                            }
                            setPositiveButton("装备"){_,_->
                                AllEquipDatabaseUtil().update(activity,e_id,cur_acc)
                                activity.finish()
                            }
                            show()
                        }
                    }
                    else{
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你完成挑战，并获得${exp}经验值,点击退出回到主页！")
                            setPositiveButton("退出"){_,_->
                                activity.finish()
                            }
                            show()
                        }
                    }
                }
                else{
                    when(cur_finish){
                        1 -> LoadAndSave().save(mg2,"cur_fight",this)
                        2 -> LoadAndSave().save(mg3,"cur_fight",this)
                    }
                    m2start.setText("继续")
                }
                //打败怪物
                m2start.isClickable = true
                m2start.isEnabled = true

                m2start.setTextColor(Color.parseColor("#2085D5"))
                map2gw.visibility = View.INVISIBLE
                m2gprog.visibility = View.INVISIBLE
                m2gb.setText("")
                m2textb.setText("")
                map2card1.visibility = View.INVISIBLE
                map2card2.visibility = View.INVISIBLE
                map2card3.visibility = View.INVISIBLE

            }
            else{
                map2card1.isClickable=false
                map2card1.isEnabled=false
                map2card2.isClickable=false
                map2card2.isEnabled=false
                map2card3.isClickable=false
                map2card3.isEnabled=false
                map2card2.setBackgroundResource(R.drawable.suo)
                map2card3.setBackgroundResource(R.drawable.suo)
                map2card2.setText("")
                map2card3.setText("")

            }
            if (LoadAndSave().load(cur_fight,this).equals("true")){
                Handler().postDelayed({
                    mfinished()
                },1100)
            }

//            map1card1.isClickable=false
//            map1card1.isEnabled=false
//            map1card2.isClickable=false
//            map1card2.isEnabled=false
//            map1card3.isClickable=false
//            map1card3.isEnabled=false
//            map1card2.setBackgroundResource(R.drawable.suo)
//            map1card3.setBackgroundResource(R.drawable.suo)
//            map1card2.setText("")
//            map1card3.setText("")
        }
        map2card2.setOnClickListener {
            map2card2.visibility = View.INVISIBLE
            val res1 = RandomSkill().action(2,this)
            if (res1<0){
                m2hurt.setText(res1.toString())
                Handler().postDelayed({
                    m2hurt.setText("")
                },400)
            }
            else{
                m2heroadd.setText("+$res1")
                Handler().postDelayed({
                    m2heroadd.setText("")
                },400)
            }

            val cur_fight = LoadAndSave().load("cur_fight",this)
            cur_life = LoadAndSave().load("hlife",this)
            m1g1_life = LoadAndSave().load(cur_fight+"life",this)
            m2gprog.progress = (m1g1_life.toInt()*100)/max_gwlife
//            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            h2textb.setText("$cur_life/$hero_max")
            m2textb.setText("$m1g1_life/$max_gwlife")
            val cur_finish = RandomSkill().finish(this)
            if (cur_finish>0){
                if (cur_finish==3){
                    LoadAndSave().save("true",map,this)
                    //完成挑战，随机爆装备
                    m2start.setText("完成")
                    m2exit.setText("退出")
                    if (GetEquip().isGetEquip()){
                        //随机装备
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val ename = GetEquip().randomEquip(this,lv)
                        val e_name = ename[0]
                        val e_id = ename[1]
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你额外获得了装备: $e_name,请选择是否穿戴")
                            setNegativeButton("丢弃"){_,_->
                                activity.finish()
                            }
                            setPositiveButton("装备"){_,_->
                                AllEquipDatabaseUtil().update(activity,e_id,cur_acc)
                                activity.finish()
                            }
                            show()
                        }
                    }
                    else{
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你完成挑战，并获得${exp}经验值,点击退出回到主页！")
                            setPositiveButton("退出"){_,_->
                                activity.finish()
                            }
                            show()
                        }
                    }
                }
                else{
                    when(cur_finish){
                        1 -> LoadAndSave().save(mg2,"cur_fight",this)
                        2 -> LoadAndSave().save(mg3,"cur_fight",this)
                    }
                    m2start.setText("继续")
                }
                //打败怪物
                m2start.isClickable = true
                m2start.isEnabled = true

                m2start.setTextColor(Color.parseColor("#2085D5"))
                map2gw.visibility = View.INVISIBLE
                m2gprog.visibility = View.INVISIBLE
                m2gb.setText("")
                m2textb.setText("")
                map2card1.visibility = View.INVISIBLE
                map2card2.visibility = View.INVISIBLE
                map2card3.visibility = View.INVISIBLE
            }
            else{
                map2card1.isClickable=false
                map2card1.isEnabled=false
                map2card2.isClickable=false
                map2card2.isEnabled=false
                map2card3.isClickable=false
                map2card3.isEnabled=false
                map2card1.setBackgroundResource(R.drawable.suo)
                map2card3.setBackgroundResource(R.drawable.suo)
                map2card1.setText("")
                map2card3.setText("")
            }
            if (LoadAndSave().load(cur_fight,this).equals("true")){
                Handler().postDelayed({
                    mfinished()
                },1100)
            }
//            map1card1.isClickable=false
//            map1card1.isEnabled=false
//            map1card2.isClickable=false
//            map1card2.isEnabled=false
//            map1card3.isClickable=false
//            map1card3.isEnabled=false
//            map1card1.setBackgroundResource(R.drawable.suo)
//            map1card3.setBackgroundResource(R.drawable.suo)
//            map1card1.setText("")
//            map1card3.setText("")
        }
        map2card3.setOnClickListener {
            map2card3.visibility = View.INVISIBLE
            val res1 = RandomSkill().action(3,this)
            if (res1<0){
                m2hurt.setText(res1.toString())
                Handler().postDelayed({
                    m2hurt.setText("")
                },400)
            }
            else{
                m2heroadd.setText("+$res1")
                Handler().postDelayed({
                    m2heroadd.setText("")
                },400)
            }

            val cur_fight = LoadAndSave().load("cur_fight",this)
            cur_life = LoadAndSave().load("hlife",this)
            m1g1_life = LoadAndSave().load(cur_fight+"life",this)
            m2gprog.progress = (m1g1_life.toInt()*100)/max_gwlife
//            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            h2textb.setText("$cur_life/$hero_max")
            m2textb.setText("$m1g1_life/$max_gwlife")
            val cur_finish = RandomSkill().finish(this)
            if (cur_finish>0){
                if (cur_finish==3){
                    LoadAndSave().save("true",map,this)
                    //完成挑战，随机爆装备
                    m2start.setText("完成")
                    m2exit.setText("退出")
                    if (GetEquip().isGetEquip()){
                        //随机装备
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val ename = GetEquip().randomEquip(this,lv)
                        val e_name = ename[0]
                        val e_id = ename[1]
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你额外获得了装备: $e_name,请选择是否穿戴")
                            setNegativeButton("丢弃"){_,_->
                                activity.finish()
                            }
                            setPositiveButton("装备"){_,_->
                                AllEquipDatabaseUtil().update(activity,e_id,cur_acc)
                                activity.finish()
                            }
                            show()
                        }
                    }
                    else{
                        UserDatabaseUtil().addexp(this,cur_acc,exp)
                        val activity = this as Activity
                        AlertDialog.Builder(this).apply {
                            setTitle("胜利")
                            setMessage("恭喜你完成挑战，并获得${exp}经验值,点击退出回到主页！")
                            setPositiveButton("退出"){_,_->
                                activity.finish()
                            }
                            show()
                        }
                    }
                }
                else{
                    when(cur_finish){
                        1 -> LoadAndSave().save(mg2,"cur_fight",this)
                        2 -> LoadAndSave().save(mg3,"cur_fight",this)
                    }
                    m2start.setText("继续")
                }
                //打败怪物
                m2start.isClickable = true
                m2start.isEnabled = true
//                m1finish.isClickable = false
//                m1finish.setTextColor(Color.parseColor("#919AA2"))
                m2start.setTextColor(Color.parseColor("#2085D5"))
                map2gw.visibility = View.INVISIBLE
                m2gprog.visibility = View.INVISIBLE
                m2gb.setText("")
                m2textb.setText("")
                map2card1.visibility = View.INVISIBLE
                map2card2.visibility = View.INVISIBLE
                map2card3.visibility = View.INVISIBLE
            }
            else{
                map2card1.isClickable=false
                map2card1.isEnabled=false
                map2card2.isClickable=false
                map2card2.isEnabled=false
                map2card3.isClickable=false
                map2card3.isEnabled=false
                map2card2.setBackgroundResource(R.drawable.suo)
                map2card1.setBackgroundResource(R.drawable.suo)
                map2card2.setText("")
                map2card1.setText("")
            }
            if (LoadAndSave().load(cur_fight,this).equals("true")){
                Handler().postDelayed({
                    mfinished()
                },1100)
            }
//            map1card1.isClickable=false
//            map1card1.isEnabled=false
//            map1card2.isClickable=false
//            map1card2.isEnabled=false
//            map1card3.isClickable=false
//            map1card3.isEnabled=false
//            map1card2.setBackgroundResource(R.drawable.suo)
//            map1card1.setBackgroundResource(R.drawable.suo)
//            map1card2.setText("")
//            map1card1.setText("")
        }

        m2exit.setOnClickListener {
            val activity = this as Activity
            activity.finish()
        }
    }
    fun mfinished(){
        val max_gwlife = LoadAndSave().load("max_gwlife",this).toInt()
        val m_res = RandomSkill().mattack(this,3)
        val cur_life = LoadAndSave().load("hlife",this)
        Handler().postDelayed({
            //                hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
            hprog.progress = (cur_life.toInt()*100)/(LoadAndSave().load("maxlife",this).toInt())
        },400)
        Handler().postDelayed({
            val cur_skill = RandomSkill().showSkill(this)
            map2card1.setText(cur_skill[0])
            map2card1.setTextColor(Color.parseColor(cur_skill[1]))
            map2card2.setText(cur_skill[2])
            map2card2.setTextColor(Color.parseColor(cur_skill[3]))
            map2card3.setText(cur_skill[4])
            map2card3.setTextColor(Color.parseColor(cur_skill[5]))
            map2card2.setBackgroundResource(R.drawable.card)
            map2card1.setBackgroundResource(R.drawable.card)
            map2card3.setBackgroundResource(R.drawable.card)
            map2card1.visibility = View.VISIBLE
            map2card2.visibility = View.VISIBLE
            map2card3.visibility = View.VISIBLE
        },1000)
        val temp = m_res[1]
        m2heroadd.setText("-$temp")
        Handler().postDelayed({
            m2heroadd.setText("")
        },400)

        if (m_res[0]==0){
            val activity = this as Activity
            AlertDialog.Builder(this).apply {
                setTitle("挑战失败")
                setMessage("生命值为0，无法继续挑战！")
                setPositiveButton("退出"){_,_->
                    activity.finish()
                }
                show()
            }
        }

        map2card1.isClickable=true
        map2card1.isEnabled=true
        map2card2.isClickable=true
        map2card2.isEnabled=true
        map2card3.isClickable=true
        map2card3.isEnabled=true
//            m1start.isClickable = true
//            m1start.isEnabled = true
//        val flagm1g1 = LoadAndSave().load("m1g1",this)
//        val flagm1g2 = LoadAndSave().load("m1g2",this)
//        val flagm1g3 = LoadAndSave().load("m1g3",this)

        val hero_max = LoadAndSave().load("maxlife",this)
        val cur_fight = LoadAndSave().load("cur_fight",this)
        val m1g1_life = LoadAndSave().load(cur_fight+"life",this)

        h2textb.setText("$cur_life/$hero_max")
        if (LoadAndSave().load(cur_fight,this).equals("true")){
            m2textb.setText("$m1g1_life/$max_gwlife")
        }

    }
}
